<template>
  <div class="main_history">
    <div class="main_history_title">
      <div class="main_history_title_1">浏览历史</div>
    </div>
    <div class="main_history_for">
      <div v-for="item in browseList" :key="item.id">
        <div v-if="item.tid==1" class="main_history_for_item">
          <div>
            <router-link :to="{
            path: '/articleDetails',
            query: {
              articleId:item.t.id
            }
          }">
              <div class="main_history_for_item_title">{{ item.t.title }}</div>
            </router-link>
          </div>
          <div class="main_history_for_item_createTime">浏览于&nbsp;&nbsp;{{ item.browseTime }}</div>
        </div>
        <div v-if="item.tid==2" class="main_history_for_item">
          <div>
            <router-link :to="{
            path: '/problemDetails',
            query: {
              id:item.t.id
            }
          }">
              <div class="main_history_for_item_title">{{ item.t.title }}</div>
            </router-link>
          </div>
          <div class="main_history_for_item_createTime">浏览于&nbsp;&nbsp;{{ item.browseTime }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref} from "vue";
import {getUserBrowseList} from "@/api/personalCenter/browse";
// @ts-ignore
import cookie from 'js-cookie'

//浏览记录列表
const browseList = ref()

//用户id
const userId = ref<number>(0)

//获取用户浏览记录
const handleUserBrowseList = async () => {
  const res = await getUserBrowseList(userId.value)
  browseList.value = res.data.data.list
  console.log(browseList.value)
}

//从cookie中获取用户id
const getUserId = () => {
  if (cookie.get('userinfo') != null) {
    userId.value = JSON.parse(cookie.get('userinfo')).id
  } else {
    userId.value = 0
  }
}

getUserId()
handleUserBrowseList()
</script>

<style lang="less" scoped>
.main_history {
  overflow: scroll;
  height: 94vh;

  .main_history_title {
    width: 220px;
    margin: auto;

    .main_history_title_1 {
      text-align: center;
      font-size: 30px;
      font-weight: bold;
      border-radius: 25px;
      background-image: linear-gradient(to top, #d9afd9 0%, #97d9e1 100%);
    }
  }

  .main_history_for {
    background-color: white;
    border-radius: 50px;
    padding-bottom: 20px;
    margin: 15px 10% 20px;

    .main_history_for_item {
      margin-left: 30px;
      margin-right: 30px;
      padding: 10px;
      border-bottom: 1px solid #f0f0f0;

      .main_history_for_item_title {
        font-size: 20px;
        font-weight: bold;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

      .main_history_for_item_title:hover {
        color: red;
      }

      .main_history_for_item_createTime {
        margin: 5px 0;
      }
    }
  }
}
</style>